package cn.uyunsky.platform.service.impl;

import cn.uyunsky.platform.config.bean.menu.PlModule;
import cn.uyunsky.platform.config.bean.menu.PlTran;
import cn.uyunsky.platform.config.bean.menu.PlTree;
import cn.uyunsky.platform.config.reader.MenuReader;
import cn.uyunsky.platform.data.UData;
import cn.uyunsky.platform.data.UDataTool;
import cn.uyunsky.platform.entity.impl.PlRole;
import cn.uyunsky.platform.entity.impl.PlUser;
import cn.uyunsky.platform.exception.PLException;
import cn.uyunsky.platform.service.IServiceAbstract;
import cn.uyunsky.platform.vo.ModelVO;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 描述: 主页面获取菜单服务,根据用户id获取角色,生成菜单 ModelVO
 * User: baiyh
 * Date: 13-6-15
 * Time: 下午11:04
 */
@Service(value = "MenuService")
@Transactional
public class MenuService extends IServiceAbstract {
    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public UData doService(UData data) throws PLException {
        String userid = (String) data.getBody().get("userid");

        PlUser example = new PlUser();
        example.setUserid(userid);
        List findRes = hibernateTemplate.findByExample(example);
        if (findRes.size() > 0) {
            PlUser user = (PlUser) findRes.get(0);
            logger.debug("用户:" + user);
            PlRole role = user.getRole();
            logger.debug("用户角色:" + role);

            List<ModelVO> moduleList = new ArrayList<ModelVO>();


            // 获取到此userid对应role的所有tran列表
            Set<String> userAlltrans = null;
            String roleflag = role.getFlag();
            logger.info("用户角色属性:{}", roleflag);

            MenuReader edit = this.getReader(MenuReader.class);

            // 如果角色是系统管理员,则默认显示全部交易,不用角色控制
            if ("1".equals(roleflag)) {
                userAlltrans = edit.getAllTrancode();
            } else {
                logger.debug("用户角色trancode:{}", role.getTrancode());
                userAlltrans = role.getTrancode();
            }
            logger.debug("userAlltrans:{}", userAlltrans);

            // 从菜单根节点查询所有TranModule列表
            PlTree tree = edit.getTree("menu");

            // 先添加tree下的modules
            Set<PlModule> treemodules = tree.getModules();
            for (PlModule module : treemodules) {

                ModelVO vo = new ModelVO();
                vo.setId(module.getId());
                vo.setExpanded(module.isExpanded());
                vo.setText(module.getText());
                Set<PlTran> treetrans = module.getTrans();
                for (PlTran tran : treetrans) {
                    logger.debug("tran:{}", tran);
                    if (userAlltrans.contains(tran.getCode())) {
                        vo.addTran(tran);
                    }
                }
                moduleList.add(vo);
            }


            Map resbody = new HashMap();
            resbody.put("moduleList", moduleList);
            return UDataTool.createSuccessResult(resbody);
        } else {
            throw new PLException("9999");
        }
    }
}
